package com.hl.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.entity.Course;
import com.hl.sfzy.entity.User;
import com.hl.sfzy.exception.AppException;
import com.hl.sfzy.mapper.UserMapper;
import com.hl.sfzy.service.UserService;
import com.hl.sfzy.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper UserMapper;


    @Override
    public User findByUsername(String username) {
        return UserMapper.selectByUsername(username);
    }



    @Override
    public User login(User user) {
        user.setUserPwd(MD5Util.encode(user.getUserPwd())); // 对密码加密
        return  UserMapper.login(user);
    }
    @Override
    public User userLogin(User user) {
        user.setUserPwd(MD5Util.encode(user.getUserPwd())); // 对密码加密
        return  UserMapper.userLogin(user);
    }

    @Override
    public List<Course> getUserCourses(String id) {
        return UserMapper.getUserCourses(id);
    }

    @Override
    public List<Course> getUserCollections(String id) {
        return UserMapper.getUserCollections(id);
    }


    @Override
    public void add(User entity) throws AppException {
        // 判断用户名是否存在
        User User = findByUsername(entity.getUserName());
        // 用户名不存在
        if (User == null) {
            entity.setUserPwd(MD5Util.encode(entity.getUserPwd())); // 对密码加密
            UserMapper.insert(entity);
        } else {
            throw new AppException("用户名已存在!");
        }
    }

    @Override
    public void edit(User entity) {
        if (entity.getUserPwd() != null && !entity.getUserPwd().equals("")) {
            entity.setUserPwd(MD5Util.encode(entity.getUserPwd())); // 对密码加密
            UserMapper.insert(entity);
        }
        UserMapper.update(entity);
    }

    @Override
    public void remove(Integer id) {
        UserMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        UserMapper.deleteBatch(ids);
    }

    @Override
    public List<User> find(User entity) {
        return UserMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, User entity) {
        //分页业务实现
        PageHelper.startPage(pageNum, pageSize);

        List<User> list = UserMapper.select(entity); // 当前页数据

        PageInfo<User> pageInfo = new PageInfo<>(list); // 分页信息对象

        /*
         * 把前台需要的数据反馈
         * 1、当前页面数据（数据列表）
         * 2、此次查询的总记录数
         */
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put(CommonConstant.PAGE_LIST, list);
        pageMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getTotal());

        return pageMap;
    }

    @Override
    public User findById(Integer id) {
         User user =UserMapper.selectById(id);
        return user;
    }

}
